﻿Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml.Linq
Imports Exp.Model
Imports Exp.DATA_Service
Imports ExpWeb.GetDataUtil

Public Class rqJoin
    Inherits BasePage

    Const MENUNAME As String = "REQJOIN"
    Private _User As REM_User

    Private Property user() As REM_User
        Get
            If Session("USER") Is Nothing Then Response.Redirect("~/login.aspx") Else _User = CType(Session("USER"), REM_User)
            Return _User
        End Get
        Set(value As REM_User)
            _User = value
        End Set
    End Property
    Const SendSuccess As String = "Send request completed."
    Const SaveSuccess As String = "Save completed."
    Const SaveError As String = "Save error !"
    Const DeleteSuccess As String = "Delete completed."
    Const DeleteError As String = "Delete not complete"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If Session("USER") Is Nothing Then
                Response.Redirect("~/login.aspx")
            Else
                user = CType(Session("USER"), REM_User)

                Dim objUSV As New Exp.DATA_Service.User_Service
                If Not objUSV.isHasRight(user.User_ID, MENUNAME) Then
                    Session("USER") = Nothing
                    Session.Clear()
                    Response.Redirect("~/login.aspx")
                End If

                MultiView1.ActiveViewIndex = 0
                BindControl()
                checkStatusRequest()
                BindGridview()

            End If
        End If

    End Sub

    Private Sub BindControl()
        Dim objOwnerSV As New Owner_Service
        Dim objUserSV As New User_Service

        Dim iUnit As Integer = 0
        Dim iCountUnit = objOwnerSV.GetByUser_ID(user.User_ID).Count
        Me.lblRequestorView0.Text = user.TitleName & "" & user.FirstName & " " & user.LastName
        Me.lblPointBalanceView0.Text = objUserSV.GetPointBalance(user.User_ID).ToString("#,###")

        Me.btnReqJoin.Visible = False
        If Not user.JoinDate Is Nothing Then
            Me.lblJoinDateView0.Text = user.JoinDate.Value.ToString("dd MMM yyyy")
            Me.lblJoinDateView0.Visible = True
            Me.txtJoin_DTView0.Visible = False

        End If

        If Not user.ExpireDate Is Nothing Then Me.lblExpireDateView0.Text = user.ExpireDate.Value.ToString("dd MMM yyyy")

        If GetDataUtil.checkIsMemberActive(user) = False Then

            Me.btnReqJoin.Visible = True
            Me.txtJoin_DTView0.Visible = True
            Me.cldJoin_DTView0.Enabled = True
            Me.lblJoinDateView0.Visible = False
            'Me.btnCalView0.Visible = True
            'Else
            'Me.btnCalView0.Visible = False
        End If


    End Sub

#Region "Gridview"
    Private Sub BindGridview()
        Dim objSV As New Owner_Service
        Dim listData = objSV.GetOwner_ByUserID(user.User_ID)
        grdView0.DataSource = listData
        grdView0.DataBind()
        Calculate()

        If GetDataUtil.checkIsMemberActive(user) = True Then
            grdView0.Columns(0).Visible = False
            'Else
            '    grdView0.Columns(0).Visible = True
        End If
    End Sub

    Private Sub checkStatusRequest()
        Dim objSvJoin As New User_RequestJoin_Service
        Dim objSvUser As New User_Service
        Dim objUSer = objSvUser.GetByID(user.User_ID)

        Dim Status As String = ""
        If Not objUSer.ExpireDate Is Nothing AndAlso Now.Date <= objUSer.ExpireDate.Value AndAlso objUSer.isActive = True Then
            Me.btnReqJoin.Visible = False
            'Me.btnCalView0.Visible = False
            Me.lblStatusView0.Text = "Is member Exchange Program"
        Else

            Dim objJoin = objSvJoin.GetByUserID(user.User_ID)

            If Not objJoin Is Nothing Then
                If Now.Date <= objJoin.ExpireDate.Value Then

                    Select Case objJoin.Status.ToUpper.Trim

                        Case "REQUEST", "APPROVED"
                            Me.lblJoinDateView0.Text = objJoin.JoinDate.Value.ToString("dd MMM yyyy")
                            Me.lblJoinDateView0.Visible = True
                            Me.lblExpireDateView0.Text = objJoin.ExpireDate.Value.ToString("dd MMM yyyy")
                            Me.lblExpireDateView0.Visible = True
                            Me.txtJoin_DTView0.Visible = False
                            Me.btnReqJoin.Visible = False
                            'Me.btnCalView0.Visible = False
                            Me.grdView0.Columns(0).Visible = False

                            If objJoin.Status.ToUpper.Trim = "REQUEST" Then Me.lblStatusView0.Text = "Waiting for approve member."
                            If objJoin.Status.ToUpper.Trim = "APPROVED" Then Me.lblStatusView0.Text = "Member Approve."
                        Case "REJECT"
                            Me.btnSaveView1.Visible = True
                            Me.btnReqJoin.Visible = True
                            'Me.btnCalView0.Visible = True
                            Me.lblStatusView0.Text = "Reject request join member."
                            If objJoin.ReasonReject.Trim <> "" Then Me.lblStatusView0.Text = Me.lblStatusView0.Text & "(" & objJoin.ReasonReject.Trim & ")"
                            grdView0.Columns(0).Visible = True
                        Case Else
                            Me.btnSaveView1.Visible = True
                            Me.btnReqJoin.Visible = True
                            'Me.btnCalView0.Visible = True
                            grdView0.Columns(0).Visible = True
                    End Select



                    'If objJoin.Status.ToUpper.Trim = "REQUEST" Then
                    '    Me.lblJoinDateView0.Text = objJoin.JoinDate.Value.ToString("dd MMM yyyy")
                    '    Me.lblJoinDateView0.Visible = True
                    '    Me.lblExpireDateView0.Text = objJoin.ExpireDate.Value.ToString("dd MMM yyyy")
                    '    Me.lblExpireDateView0.Visible = True
                    '    Me.txtJoin_DTView0.Visible = False
                    '    Me.btnReqJoin.Visible = False
                    '    Me.btnCalView0.Visible = False
                    '    Me.lblStatusView0.Text = "Waiting for approve member."
                    '    Me.grdView0.Columns(0).Visible = False
                    'End If

                    'If objJoin.Status.ToUpper.Trim = "REJECT" Then
                    '    Me.btnReqJoin.Visible = True
                    '    Me.btnCalView0.Visible = True
                    '    Me.lblStatusView0.Text = "Reject request join member."
                    '    If objJoin.ReasonReject.Trim <> "" Then Me.lblStatusView0.Text = Me.lblStatusView0.Text & "(" & objJoin.ReasonReject.Trim & ")"
                    'End If

                    'If objJoin.Status.ToUpper.Trim = "APPROVED" Then
                    '    Me.lblJoinDateView0.Text = objJoin.JoinDate.Value.ToString("dd MMM yyyy")
                    '    Me.lblJoinDateView0.Visible = True
                    '    Me.lblExpireDateView0.Text = objJoin.ExpireDate.Value.ToString("dd MMM yyyy")
                    '    Me.lblExpireDateView0.Visible = True
                    '    Me.btnReqJoin.Visible = False
                    '    Me.btnCalView0.Visible = False
                    '    Me.lblStatusView0.Text = "Member Approve"
                    '    Me.grdView0.Columns(0).Visible = False
                    'End If

                Else
                    Me.lblStatusView0.Text = "Not join Exchange Program"
                    Me.grdView0.Columns(0).Visible = True
                End If

            Else

                Me.lblStatusView0.Text = "Not join Exchange Program"
                Me.grdView0.Columns(0).Visible = True

            End If



        End If




    End Sub

    Protected Sub grdView0_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles grdView0.RowCommand
        Dim commandArgs As String() = e.CommandArgument.ToString().Split(",")
        Dim Unit_ID As Integer = CInt(commandArgs(0))

        If e.CommandName.ToString.ToUpper = "DETAIL" Then
            'ClearFormView2()
            'BindDataForView(User_ID)
            MultiView1.ActiveViewIndex = 1
            'hdFromView3.Value = 1
            Me.ucUnitView1.UnitID = unit_ID
            Me.ucUnitView1.BindData()
            btnSave1View1.Visible = False
        ElseIf e.CommandName.ToString.ToUpper = "EDIT" Then
            MultiView1.ActiveViewIndex = 1
            'hdFromView3.Value = 1
            Me.ucUnitView1.UnitID = Unit_ID
            Me.ucUnitView1.BindData(True)
            btnSave1View1.Visible = True
        End If
    End Sub

    Private Sub grdView0_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles grdView0.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If Me.btnReqJoin.Visible = True And Me.btnReqJoin.Enabled = True Then
                CType(e.Row.FindControl("txtQouta"), TextBox).Enabled = True
            Else
                CType(e.Row.FindControl("txtQouta"), TextBox).Enabled = False
            End If
        End If


        'If Trim(CType(e.Row.DataItem, DataRowView).Item("isNew") & "") <> "" Then
        '    e.Row.ToolTip = "การเพิ่ม/แก้ไข ยังไม่ได้ถูกบันทึก"
        '    Dim btnDelete As ImageButton = CType(e.Row.FindControl("btnDelete"), ImageButton)
        '    btnDelete.Visible = True
        'End If

        'If CType(e.Row.DataItem, DataRowView).Item("ST").ToString <> "1" Then
        '    e.Row.ForeColor = Drawing.Color.LightGray
        'End If

        'Dim lblstDate As Label = e.Row.FindControl("lblstDate")
        'Dim rateDate As Date = CType(e.Row.DataItem, DataRowView).Item("EFF_DT")
        'lblstDate.Text = rateDate.ToString("dd/MM/") & IIf(rateDate.Year > 2500, rateDate.Year, rateDate.Year + 543)


    End Sub

    Private Sub grdView0_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles grdView0.RowEditing

    End Sub

#End Region

   
    Protected Sub btnReqJoin_Click(sender As Object, e As EventArgs) Handles btnReqJoin.Click
        Dim sError As String = ""
        Dim joindate As Date

        Calculate(True)

        If Me.grdView0.Rows.Count < 1 Then
            sError += "You not have units for join.\n"
            JavaScript.AlertMessage(Me.Page, sError)
            Exit Sub
        End If

        If CInt(Me.lblPointBalanceView0.Text) = 0 Then
            sError += "Please input quota of units.\n"
            JavaScript.AlertMessage(Me.Page, sError)
            Exit Sub
        End If


        If Me.txtJoin_DTView0.Text.Trim <> "" Then
            joindate = Date.ParseExact(Me.txtJoin_DTView0.Text.Trim, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo) 'CDate(Me.txtJoin_DTView1.Text.Trim)
            If Now.Date.AddDays(30).Date > joindate Then
                sError += "Please input join date at least 30 day from today.\n"
            Else
                Dim sEmailApprover As String = ""

                Try

                    Dim obj As New RET_User_ReqeuestJoin

                    obj.Requester_ID = user.User_ID
                    obj.JoinDate = joindate
                    obj.ExpireDate = joindate.AddDays(365)
                    obj.Status = "REQUEST"
                    obj.CreateUser = user.User_ID
                    obj.CreateDate = Now

                    Dim sv As New User_RequestJoin_Service(obj)
                    sv.Add()

                    JavaScript.AlertMessage(Me.Page, SendSuccess)
                    BindControl()
                    checkStatusRequest()
                    BindGridview()
                Catch ex As Exception
                    JavaScript.AlertMessage(Me.Page, ex.Message)
                    Exit Sub
                End Try


                Dim sBody As String = "" & user.User_ID
                If sEmailApprover.Trim <> "" Then sEmailApprover = sEmailApprover.Substring(0, sEmailApprover.Length - 2)

                Try
                    '  MailService.SendMail(user.Email, sEmailApprover, MailService.SendMailFor.RejectJoin, sBody, "", "", True)
                Catch ex As Exception

                End Try


            End If
        Else
            sError += "Please input join date at least 30 day from today.\n"
        End If
        If sError <> "" Then
            JavaScript.AlertMessage(Me.Page, sError)
            Exit Sub
        End If

    End Sub

    Protected Sub btnCalView0_Click(sender As Object, e As EventArgs) Handles btnCalView0.Click
        Calculate()
    End Sub

    Private Sub Calculate(Optional ByVal isSave As Boolean = False)
        Dim sv As New Owner_Service
        Dim lblPointBalance As Double = 0
        For i As Integer = 0 To grdView0.Rows.Count - 1

            Dim TotalPoint As Double = 0
            CType(grdView0.Rows(i).FindControl("lblGetPointView0"), Label).Text = CInt(CType(grdView0.Rows(i).FindControl("lblPointPerNightView0"), Label).Text) * CInt(CType(grdView0.Rows(i).FindControl("txtQouta"), TextBox).Text)

            lblPointBalance += CInt(CType(grdView0.Rows(i).FindControl("lblGetPointView0"), Label).Text)
            CType(grdView0.Rows(i).FindControl("lblGetPointView0"), Label).Text = CInt(CType(grdView0.Rows(i).FindControl("lblGetPointView0"), Label).Text).ToString("#,###")
            If isSave = True Then
                Dim unit_id As Integer = CInt(CType(grdView0.Rows(i).FindControl("lblUnit_ID"), Label).Text.Trim)
                sv.UpdateQaota(unit_id, user.User_ID, CInt(CType(grdView0.Rows(i).FindControl("txtQouta"), TextBox).Text), user.User_ID)
            End If
        Next

        Me.lblPointBalanceView0.Text = lblPointBalance.ToString("#,###")
    End Sub


    'Private Function GetListUnitQuota() As Array(2)
    '    Dim arrUnit As New ArrayList(2)
    '    arrUnit.Add()
    'End Function

    Private Sub btnBackView1_Click(sender As Object, e As EventArgs) Handles btnBackView1.Click
        Me.MultiView1.ActiveViewIndex = 0
    End Sub

    Private Sub btnBack1View1_Click(sender As Object, e As EventArgs) Handles btnBack1View1.Click
        Me.MultiView1.ActiveViewIndex = 0
    End Sub

    Private Sub btnSaveView1_Click(sender As Object, e As EventArgs) Handles btnSaveView1.Click
        If ucUnitView1.SaveShareDate(user.User_ID) = False Then Exit Sub

        MultiView1.ActiveViewIndex = 0
        BindControl()
        checkStatusRequest()
        BindGridview()
    End Sub

    Private Sub btnSave1View1_Click(sender As Object, e As EventArgs) Handles btnSave1View1.Click
        If ucUnitView1.SaveShareDate(user.User_ID) = False Then Exit Sub

        MultiView1.ActiveViewIndex = 0
        BindControl()
        checkStatusRequest()
        BindGridview()
    End Sub

    Protected Sub btn_Cancel_Click(sender As Object, e As EventArgs) Handles btn_Cancel.Click
        Me.MultiView1.ActiveViewIndex = 0
    End Sub

    Protected Sub btn_Save_Click(sender As Object, e As EventArgs) Handles btn_Save.Click
       
        ' Update()
    End Sub



End Class